<?php
namespace pfp\lib {

    \defined( '_JEXEC' ) or die( 'Restricted access' );

    /**
     * Description of Autoloader
     *
     * @todo 
     * @author George Zeakis<zeageorge@google.com>
     * 
     */
    class Autoloader {
        
        /**
         *
         * @var Autoloader
         */
        private static $_instance = \NULL;
        
        /**
         *
         * @var array
         */
        private $_namespaces = array();

        /**
         * 
         * @param array|string $namespace
         * @param string $folderPath
         * @throws \Exception
         */
        public static function registerNamespace($namespace, $folderPath=""){
            if (\is_array($namespace)){
                foreach ($namespace as $key => $value) {
                    if (!\array_key_exists($key, self::$_instance->_namespaces) && !isset(self::$_instance->_namespaces["$key"])){
                        self::$_instance->_namespaces["$key"] = (string)$value;
                    }
                }
            } elseif (\is_string($namespace)){
                if (!\array_key_exists($namespace, self::$_instance->_namespaces) && !isset(self::$_instance->_namespaces["$namespace"])){
                    self::$_instance->_namespaces["$namespace"] = (string)$folderPath;
                }
            } else {
                throw new \Exception('Could not register Namespace.');
            }
        }

        public static function unregisterNamespace($namespace){
              if (isset(self::$_instance->_namespaces["$namespace"]) || \array_key_exists($namespace, self::$_instance->_namespaces)){
                  unset(self::$_instance->_namespaces["$namespace"]);
              }
        }


        public function autoloader($class_name){
            if (\class_exists($class_name, \FALSE)) { return \TRUE; }

            $className_startsAt = \strrpos($class_name,"\\");
            if ($className_startsAt===\FALSE){ // character \ not found in $class_name
                return \FALSE;
            } else {
                $className = \substr($class_name, $className_startsAt+1);
                $ns = \substr($class_name, 0, $className_startsAt+1);
//                echo "class_name: ".$className."<br />\n";
//                echo "namespace : ".$ns."<br />\n";
                
                if (!\array_key_exists($ns, self::$_instance->_namespaces)){
                    echo "namespace : ".$ns." NOT FOUND.<br />\n";
//                    echo "<pre>";
//                    \var_dump(self::$_instance->_namespaces);
                    return \FALSE;
                }
                
                if (!\is_readable(self::$_instance->_namespaces["$ns"].$className.".php")){
                    echo "File is NOT readable or DOES NOT exists, class_name: ".self::$_instance->_namespaces["$ns"].$className.".php"."<br />\n";
                    return \FALSE;
                }
                
                require_once self::$_instance->_namespaces["$ns"].$className.".php";
                return \TRUE;
            }
        }

        /**
         * 
         * @return pfp\lib\Autoloader
         * @throws \Exception
         */
        public static function Init(){
            if( self::$_instance === \NULL ) { 
                self::$_instance = new self();
                if (!\spl_autoload_register(array(self::$_instance, 'autoloader'))) {
                    throw new \Exception('Could not set Autoload function');
                }
            }
            return self::$_instance;
        }



        protected function __construct(){}

        protected function __clone(){}

        public function __destruct() {
            \spl_autoload_unregister(array(self::$_instance, 'autoloader'));
        }
    }
}
?>
